// Copyright 2009 Thiago H. de Paula Figueiredo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package br.com.arsmachina.authorization.ioc;

import java.util.Map;

import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;

import br.com.arsmachina.authorization.Authorizer;
import br.com.arsmachina.authorization.SingleTypeAuthorizer;
import br.com.arsmachina.authorization.internal.AuthorizerImpl;

/**
 * Generic Authorization module for Tapestry-IoC.
 * 
 * @author Thiago H. de Paula Figueiredo
 */
public class GenericAuthorizationModule {
	
	/**
	 * Symbol that defines if the authorization service will be strict
	 * (denying access to objects and classes without a configured {@link SingleTypeAuthorizer}.
	 * or not. The default value is true.
	 */
	final public static String STRICT_AUTHORIZATION_SERVICE = "authorization.strict";

	/**
	 * Builds the <code>DefaultAuthorizer</code> service.
	 * 
	 * @param contributions a {@link Map} of ({@link Class}, {@link SingleTypeAuthorizer}). 
	 */
	@SuppressWarnings("unchecked")
	public static Authorizer buildAuthorizer(
			Map<Class, SingleTypeAuthorizer> contributions,
			@Inject @Symbol(STRICT_AUTHORIZATION_SERVICE)
			boolean strict) {

		return new AuthorizerImpl(contributions, strict);

	}
	
    /**
     * Contributes factory defaults for this module.
     *
     * @see TapestryModule#contributeClasspathAssetAliasManager(org.apache.tapestry5.ioc.MappedConfiguration, String,
     *      String, String)
     */
    public static void contributeFactoryDefaults(MappedConfiguration<String, String> configuration) {
    	configuration.add(STRICT_AUTHORIZATION_SERVICE, "false");
    }

}
